package operation;

import book.Book;
import book.BookList;

import java.util.Scanner;

public class AddOperation implements IOperation{
    @Override
    public void work(BookList booklist) {
        System.out.println("新增图书...");
        System.out.println("请输入你要新增的图书的书名：");
        Scanner scanner = new Scanner(System.in);
        String name = scanner.nextLine();

        int currentSize = booklist.getUsedSize();
        //遍历书架
        for (int i = 0; i < currentSize; i++) {
            Book book = booklist.getBook(i);
            if(name.equals(book.getName())) {
                System.out.println("这本书已存在，不再进行新增...");
                System.out.println(book);
                return;
            }
        }
        System.out.println("请输入你要新增的图书的作者：");
        String author = scanner.nextLine();
        System.out.println("请输入你要新增的图书的价格：");
        int price = scanner.nextInt();
        scanner.nextLine();
        System.out.println("请输入你要新增的图书的类别：");
        String type = scanner.nextLine();

        Book book = new Book(name,author,price,type);

        booklist.setBook(currentSize,book);

        booklist.setUsedSize(currentSize+1);

        System.out.println("新增成功！！！");
    }
}
